package com.dk.service.specification;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.specification.TbSpecificationMapper;
import com.dk.mapper.specification.TbSpecificationOptionMapper;
import com.dk.pojo.specification.TbSpecification;
import com.dk.pojo.specification.TbSpecificationOption;
import com.dk.pojo.specification.TbSpecificationOptionExample;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private TbSpecificationMapper specificationMapper;
    @Resource
    private TbSpecificationOptionMapper optionMapper;

    /**
     * 获取规格列表
     * @return
     */
    @Override
    public List<TbSpecification> listSpecification() {
        return specificationMapper.selectByExample(null);
    }

    @Override
    public List<TbSpecificationOption> listOptionBySpecId(Long id) {
        TbSpecificationOptionExample spec = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = spec.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> options = optionMapper.selectByExample(spec);
        return options;
    }

    @Override
    public int deleteSpecificationByIds(Long[] ids) {
        return specificationMapper.deleteSpecificationByIds(ids);
    }

    @Override
    public int updateSpecificationInfo(TbSpecification tbSpecification, String options) {
        // 修改规格表
        int i = specificationMapper.updateByPrimaryKeySelective(tbSpecification);
        // 根据规格id删除符合条件的选项
        TbSpecificationOptionExample specificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = specificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        int i1 = optionMapper.deleteByExample(specificationOptionExample);
        // 将options 转成list
        List<TbSpecificationOption> list = JSONObject.parseArray(options, TbSpecificationOption.class);
        // 添加规格表
        for (TbSpecificationOption option : list) {
            option.setSpecId(tbSpecification.getId());
            int i2 = optionMapper.insertSelective(option);
        }
        return 0;
    }

    /**
     * 查询规格 和 选项表
     * @param id
     * @return map 里面存放着查询结果
     */
    @Override
    public Map<String, Object> getSpecificationAndOption(Long id) {
        // 查询规格表
        TbSpecification specification = specificationMapper.selectByPrimaryKey(id);
        // 查询选项表
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = optionExample.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> options = optionMapper.selectByExample(optionExample);
        // 将结果存到map中
        Map<String,Object> map = new HashMap<>();
        map.put("specification",specification);
        map.put("options",options);
        return map;
    }

    /**
     * 添加规格表 并添加规格选项表
     * @param specName
     * @param options
     * @return
     */
    @Transactional
    @Override
    public int addSpecificationAndOptions(String specName, String options) {
        // 讲字符串转成json再转成list
        List<TbSpecificationOption> optionList = JSONObject.parseArray(options, TbSpecificationOption.class);
        System.out.println(optionList);
        TbSpecification specification = new TbSpecification();
        specification.setSpecName(specName);
        // 添加规格并返回主键
        int i = specificationMapper.insertSpecificationResultId(specification);
        for (TbSpecificationOption option : optionList) {
            option.setSpecId(specification.getId());
            // 添加规格选项表
            int i1 = optionMapper.insertSelective(option);
        }
        return 0;
    }
}
